<template>
  <div class="site">
    <div class="back">
      <back-nav
        :leftArrow="true"
        :title="'地址列表'"
        :leftText="'返回'"
      ></back-nav>
    </div>
    <div class="palcehode"></div>
    <div class="noneList" v-if="list.length < 1">
      暂时无地址,请添加你的地址!
    </div>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      :disabled-list="disabledList"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>
<script>
import backNav from "../components/BackNav.vue";
export default {
  name: "site",
  data() {
    return {
      chosenAddressId: "1",
      list: [],
      disabledList: [],
    };
  },
  components: {
    backNav,
  },
  methods: {
    onAdd() {
      this.$router.push({
        name: "Edit",
        query: {
          id: 100,
        },
      });
    },
    // 查询地址
    inquireAddress() {
      this.axios({
        method: "get",
        url: "/findAddress",
      })
        .then((res) => {
          if (res.data.code === 20000) {
            let address = "";
            let SiteFlag = "";
            res.data.result.forEach((item) => {
              address =
                item.province +
                item.city +
                item.county +
                " " +
                item.addressDetail;
              if (item.isDefault == 1) {
                SiteFlag = true;
              } else {
                SiteFlag = false;
              }
              this.list.push({
                id: item.id,
                name: item.name,
                tel: item.tel,
                aid: item.aid,
                postalCode: item.postalCode,
                isDefault: SiteFlag,
                address,
              });
            });
            // 设为默认地址
            for (let i = this.list.length - 1; i >= 0; i--) {
              if (this.list[i].isDefault) {
                this.list.unshift(this.list[i]);
                this.list.splice(i + 1, 1);
                break;
              }
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onEdit(item) {
      this.$router.push({
        name: "Edit",
        query: {
          id: 200,
          aid: item.aid,
        },
      });
    },
  },
  created() {
    this.inquireAddress();
  },
};
</script>
<style lang="less" scoped>
@import url("../assets/base.less");
.site {
  .back {
    position: static;
  }
  .palcehode {
    height: 100px;
    width: 100%;
    background-color: @mainColor;
  }
  .noneList {
    height: 420px;
    line-height: 450px;
    font-size: 16px;
    text-align: center;
  }
}
/deep/ .van-radio__icon .van-icon {
  width: 0;
  height: 0;
}
/deep/ .van-radio-group {
  transform: translateY(-50px);
}
/deep/ .van-address-list__bottom {
  bottom: 0;
}
/deep/ .van-tag--danger{
  background-color: @mainColor;;
}
/deep/ .van-button--danger {
  background-color: @mainColor;
  border-color: @mainColor;
}
</style>